package com.example.securitydemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    @Lazy
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 这里简单返回一个硬编码的用户，实际应用中应从数据库或其他数据源加载用户信息
        if ("user".equals(username)) {
            String rawPassword = "password";
            String encodedPassword = passwordEncoder.encode(rawPassword);
            return new User("user", encodedPassword, new ArrayList<>());
        }
        throw new UsernameNotFoundException("User not found with username: " + username);
    }
}